// 5. 输入处理模块 (已修正 - 使用新的消抖逻辑)
module input_handler(
    input clk,
    input rst_n,
    input [8:0] key_inputs,  // 9个按键输入 key_inputs[0]对应数字1
    input [3:0] current_digit,
    input [1:0] game_state,
    output reg input_correct,
    output reg input_error,
    output reg digit_advance
);
    parameter PLAYING = 2'b01;
    // 状态机状态定义
    parameter KEY_IDLE = 2'b00;         // 等待按键按下
    parameter KEY_WAIT_RELEASE = 2'b01; // 已处理按键，等待其释放
    reg [1:0] key_state;
    
    // 去抖计时参数
    parameter DEBOUNCE_TIME = 16'd10000; 
    
    // 为每个按键创建消抖电路
    reg [1:0] button_sync_0, button_sync_1, button_sync_2, button_sync_3, button_sync_4;
    reg [1:0] button_sync_5, button_sync_6, button_sync_7, button_sync_8;
    
    reg [15:0] debounce_counter_0, debounce_counter_1, debounce_counter_2, debounce_counter_3, debounce_counter_4;
    reg [15:0] debounce_counter_5, debounce_counter_6, debounce_counter_7, debounce_counter_8;
    
    reg button_stable_0, button_stable_1, button_stable_2, button_stable_3, button_stable_4;
    reg button_stable_5, button_stable_6, button_stable_7, button_stable_8;
    
    reg button_stable_prev_0, button_stable_prev_1, button_stable_prev_2, button_stable_prev_3, button_stable_prev_4;
    reg button_stable_prev_5, button_stable_prev_6, button_stable_prev_7, button_stable_prev_8;
    
    wire key_pressed_edge_0, key_pressed_edge_1, key_pressed_edge_2, key_pressed_edge_3, key_pressed_edge_4;
    wire key_pressed_edge_5, key_pressed_edge_6, key_pressed_edge_7, key_pressed_edge_8;
    
    // 按键0的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) 
        begin
            button_sync_0 <= 2'b11;
        end 
        else 
        begin
            button_sync_0 <= {button_sync_0[0], key_inputs[0]};
        end
    end
    
    always @(posedge clk or negedge rst_n) 
    begin
        if (!rst_n) 
        begin
            debounce_counter_0 <= 16'd0;
            button_stable_0 <= 1'b1;
        end
        else
        begin
            if (button_sync_0[1] != button_stable_0) begin
                if (debounce_counter_0 == DEBOUNCE_TIME - 1) 
                begin
                    button_stable_0 <= button_sync_0[1];
                    debounce_counter_0 <= 16'd0;
                end 
                else 
                begin
                    debounce_counter_0 <= debounce_counter_0 + 16'd1;
                end
            end 
            else 
            begin
                debounce_counter_0 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_0 <= 1'b1;
        end else begin
            button_stable_prev_0 <= button_stable_0;
        end
    end
    
    assign key_pressed_edge_0 = button_stable_prev_0 && !button_stable_0;
    
    // 按键1的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_1 <= 2'b11;
        end else begin
            button_sync_1 <= {button_sync_1[0], key_inputs[1]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_1 <= 16'd0;
            button_stable_1 <= 1'b1;
        end else begin
            if (button_sync_1[1] != button_stable_1) begin
                if (debounce_counter_1 == DEBOUNCE_TIME - 1) begin
                    button_stable_1 <= button_sync_1[1];
                    debounce_counter_1 <= 16'd0;
                end else begin
                    debounce_counter_1 <= debounce_counter_1 + 16'd1;
                end
            end else begin
                debounce_counter_1 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_1 <= 1'b1;
        end else begin
            button_stable_prev_1 <= button_stable_1;
        end
    end
    
    assign key_pressed_edge_1 = button_stable_prev_1 && !button_stable_1;
    
    // 按键2的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_2 <= 2'b11;
        end else begin
            button_sync_2 <= {button_sync_2[0], key_inputs[2]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_2 <= 16'd0;
            button_stable_2 <= 1'b1;
        end else begin
            if (button_sync_2[1] != button_stable_2) begin
                if (debounce_counter_2 == DEBOUNCE_TIME - 1) begin
                    button_stable_2 <= button_sync_2[1];
                    debounce_counter_2 <= 16'd0;
                end else begin
                    debounce_counter_2 <= debounce_counter_2 + 16'd1;
                end
            end else begin
                debounce_counter_2 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_2 <= 1'b1;
        end else begin
            button_stable_prev_2 <= button_stable_2;
        end
    end
    
    assign key_pressed_edge_2 = button_stable_prev_2 && !button_stable_2;
    
    // 按键3的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_3 <= 2'b11;
        end else begin
            button_sync_3 <= {button_sync_3[0], key_inputs[3]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_3 <= 16'd0;
            button_stable_3 <= 1'b1;
        end else begin
            if (button_sync_3[1] != button_stable_3) begin
                if (debounce_counter_3 == DEBOUNCE_TIME - 1) begin
                    button_stable_3 <= button_sync_3[1];
                    debounce_counter_3 <= 16'd0;
                end else begin
                    debounce_counter_3 <= debounce_counter_3 + 16'd1;
                end
            end else begin
                debounce_counter_3 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_3 <= 1'b1;
        end else begin
            button_stable_prev_3 <= button_stable_3;
        end
    end
    
    assign key_pressed_edge_3 = button_stable_prev_3 && !button_stable_3;
    
    // 按键4的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_4 <= 2'b11;
        end else begin
            button_sync_4 <= {button_sync_4[0], key_inputs[4]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_4 <= 16'd0;
            button_stable_4 <= 1'b1;
        end else begin
            if (button_sync_4[1] != button_stable_4) begin
                if (debounce_counter_4 == DEBOUNCE_TIME - 1) begin
                    button_stable_4 <= button_sync_4[1];
                    debounce_counter_4 <= 16'd0;
                end else begin
                    debounce_counter_4 <= debounce_counter_4 + 16'd1;
                end
            end else begin
                debounce_counter_4 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_4 <= 1'b1;
        end else begin
            button_stable_prev_4 <= button_stable_4;
        end
    end
    
    assign key_pressed_edge_4 = button_stable_prev_4 && !button_stable_4;
    
    // 按键5的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_5 <= 2'b11;
        end else begin
            button_sync_5 <= {button_sync_5[0], key_inputs[5]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_5 <= 16'd0;
            button_stable_5 <= 1'b1;
        end else begin
            if (button_sync_5[1] != button_stable_5) begin
                if (debounce_counter_5 == DEBOUNCE_TIME - 1) begin
                    button_stable_5 <= button_sync_5[1];
                    debounce_counter_5 <= 16'd0;
                end else begin
                    debounce_counter_5 <= debounce_counter_5 + 16'd1;
                end
            end else begin
                debounce_counter_5 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_5 <= 1'b1;
        end else begin
            button_stable_prev_5 <= button_stable_5;
        end
    end
    
    assign key_pressed_edge_5 = button_stable_prev_5 && !button_stable_5;
    
    // 按键6的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_6 <= 2'b11;
        end else begin
            button_sync_6 <= {button_sync_6[0], key_inputs[6]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_6 <= 16'd0;
            button_stable_6 <= 1'b1;
        end else begin
            if (button_sync_6[1] != button_stable_6) begin
                if (debounce_counter_6 == DEBOUNCE_TIME - 1) begin
                    button_stable_6 <= button_sync_6[1];
                    debounce_counter_6 <= 16'd0;
                end else begin
                    debounce_counter_6 <= debounce_counter_6 + 16'd1;
                end
            end else begin
                debounce_counter_6 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_6 <= 1'b1;
        end else begin
            button_stable_prev_6 <= button_stable_6;
        end
    end
    
    assign key_pressed_edge_6 = button_stable_prev_6 && !button_stable_6;
    
    // 按键7的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_7 <= 2'b11;
        end else begin
            button_sync_7 <= {button_sync_7[0], key_inputs[7]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_7 <= 16'd0;
            button_stable_7 <= 1'b1;
        end else begin
            if (button_sync_7[1] != button_stable_7) begin
                if (debounce_counter_7 == DEBOUNCE_TIME - 1) begin
                    button_stable_7 <= button_sync_7[1];
                    debounce_counter_7 <= 16'd0;
                end else begin
                    debounce_counter_7 <= debounce_counter_7 + 16'd1;
                end
            end else begin
                debounce_counter_7 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_7 <= 1'b1;
        end else begin
            button_stable_prev_7 <= button_stable_7;
        end
    end
    
    assign key_pressed_edge_7 = button_stable_prev_7 && !button_stable_7;
    
    // 按键8的消抖逻辑
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_sync_8 <= 2'b11;
        end else begin
            button_sync_8 <= {button_sync_8[0], key_inputs[8]};
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            debounce_counter_8 <= 16'd0;
            button_stable_8 <= 1'b1;
        end else begin
            if (button_sync_8[1] != button_stable_8) begin
                if (debounce_counter_8 == DEBOUNCE_TIME - 1) begin
                    button_stable_8 <= button_sync_8[1];
                    debounce_counter_8 <= 16'd0;
                end else begin
                    debounce_counter_8 <= debounce_counter_8 + 16'd1;
                end
            end else begin
                debounce_counter_8 <= 16'd0;
            end
        end
    end
    
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            button_stable_prev_8 <= 1'b1;
        end else begin
            button_stable_prev_8 <= button_stable_8;
        end
    end
    
    assign key_pressed_edge_8 = button_stable_prev_8 && !button_stable_8;
    
    // 组合所有按键的边沿信号
    wire [8:0] key_pressed_edge;
    assign key_pressed_edge = {key_pressed_edge_8, key_pressed_edge_7, key_pressed_edge_6, 
                               key_pressed_edge_5, key_pressed_edge_4, key_pressed_edge_3,
                               key_pressed_edge_2, key_pressed_edge_1, key_pressed_edge_0};
    
    // 将按键边沿信号转换为对应的数字
    reg [3:0] pressed_digit;
    reg key_detected; // 标志是否有有效按键被按下
    
    always @(*) begin
        key_detected = 0;
        pressed_digit = 0;
        case (key_pressed_edge)
            9'b000000001: begin key_detected = 1; pressed_digit = 1; end
            9'b000000010: begin key_detected = 1; pressed_digit = 2; end
            9'b000000100: begin key_detected = 1; pressed_digit = 3; end
            9'b000001000: begin key_detected = 1; pressed_digit = 4; end
            9'b000010000: begin key_detected = 1; pressed_digit = 5; end
            9'b000100000: begin key_detected = 1; pressed_digit = 6; end
            9'b001000000: begin key_detected = 1; pressed_digit = 7; end
            9'b010000000: begin key_detected = 1; pressed_digit = 8; end
            9'b100000000: begin key_detected = 1; pressed_digit = 9; end
            default:      begin key_detected = 0; pressed_digit = 0; end
        endcase
    end
    
    // 检测所有按键是否都已释放
    wire all_keys_released;
    wire [8:0] button_stable_combined;
    assign button_stable_combined = {button_stable_8, button_stable_7, button_stable_6,
                                     button_stable_5, button_stable_4, button_stable_3,
                                     button_stable_2, button_stable_1, button_stable_0};
    assign all_keys_released = (button_stable_combined == 9'h1FF);
    
    // 核心状态机和输入验证逻辑
    always @(posedge clk or negedge rst_n) 
    begin
        if (!rst_n) begin
            input_correct <= 0;
            input_error <= 0;
            digit_advance <= 0;
            key_state <= KEY_IDLE;
        end else begin
            // 默认将脉冲信号清零
            input_correct <= 0;
            digit_advance <= 0;
            // 注意：input_error不再默认清零，它将作为电平信号被状态机控制
            case (key_state)
                KEY_IDLE: begin
                    // 只有在空闲状态、游戏进行中且检测到有效按键时才响应
                    if (game_state == PLAYING && key_detected) 
                    begin
                        if (pressed_digit == current_digit) 
                        begin
                            input_correct <= 1; // 产生一个周期的正确脉冲
                            digit_advance <= 1; // 产生一个周期的进位脉冲
                            input_error <= 0;   // 确保错误标志为低
                        end 
                        else 
                        begin
                            input_error <= 1;   // 拉高错误标志电平
                        end
                        key_state <= KEY_WAIT_RELEASE; // 进入等待释放状态，锁定输入
                    end
                end
                KEY_WAIT_RELEASE: 
                begin
                    // 在此状态下，忽略所有新的按键事件
                    // 只等待所有按键都被释放
                    if (all_keys_released) 
                    begin
                        input_error <= 0; // 当按键释放后，清除错误标志电平
                        key_state <= KEY_IDLE; // 返回空闲状态，准备接收下一次按键
                    end
                end
                default: begin
                    key_state <= KEY_IDLE;
                end
            endcase
        end
    end
endmodule